//
//  JourneyButtonTableViewCell.m
//  ITrip
//
//  Created by 1564335 on 15-6-1.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "JourneyButtonTableViewCell.h"
#import <UIImageView+AFNetworking.h>
@implementation JourneyButtonTableViewCell

- (void)dealloc
{
    [_titleLabel release];
    [_descriptionLabel release];
    [_image_url release];
    [_imageScrollView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createCell];
    }
    return self;
}

- (void)createCell
{
    self.titleLabel = [[UILabel alloc] init];
    self.descriptionLabel = [[UILabel alloc] init];
    self.imageScrollView = [[UIScrollView alloc] init];
    
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.descriptionLabel];
    [self.contentView addSubview:self.imageScrollView];
    
    [self.titleLabel release];
    [self.descriptionLabel release];
    [self.imageScrollView release];
}


- (void)setSectionsParser:(StrategySectionsParser *)sectionsParser
{
    self.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    self.titleLabel.frame = CGRectMake(20, 20, self.contentView.frame.size.width - 40, 40);
    self.descriptionLabel.frame = CGRectMake(20, 70, self.contentView.frame.size.width - 40, 40);
    self.titleLabel.text = sectionsParser.strategy_third_title;
    self.descriptionLabel.text = sectionsParser.strategy_description;
    self.descriptionLabel.font = [UIFont systemFontOfSize:20];
    self.descriptionLabel.numberOfLines = 1000;
    [self.descriptionLabel sizeToFit];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:20] forKey:NSFontAttributeName];
    CGRect frame = [sectionsParser.strategy_description boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width - 40, 3000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    CGFloat photosWidth = 0;
    for (int i = 0; i < sectionsParser.photos.count; i++) {
        StrategyPhotosParser *photosParser = [sectionsParser.photos objectAtIndex:i];
        photosWidth += 142 * (photosParser.strategy_image_width / (CGFloat)photosParser.strategy_image_height) + 8;
    }
    if (sectionsParser.photos.count != 0) {
        self.imageScrollView.frame = CGRectMake(20, self.descriptionLabel.frame.origin.y + frame.size.height, self.contentView.frame.size.width - 40, 150);
//        self.imageScrollView.backgroundColor = [UIColor redColor];
        self.imageScrollView.contentSize = CGSizeMake(photosWidth, 150);
    }
    
    CGFloat photoLength = 0;
    for (StrategyPhotosParser *photo in sectionsParser.photos) {
        if (photo.strategy_image_url.length != 0) {
            self.image_url = [[UIImageView alloc] init];
            self.image_url.frame = CGRectMake(photoLength, 0, photo.strategy_image_width / (CGFloat)photo.strategy_image_height * 142, 150);
            [self.image_url setImageWithURL:[NSURL URLWithString:photo.strategy_image_url] placeholderImage:[UIImage imageNamed:@"defaultPicture"]];
            [self.imageScrollView addSubview:self.image_url];
            [self.image_url release];
        }
        photoLength += photo.strategy_image_width / (CGFloat)photo.strategy_image_height * 150;
    }
    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
